package register

import (
	"errors"
	"gitee.com/huanminabc/go-StrMathCalculate/structure"
	"gitee.com/huanminabc/go-utils/common"
)

var Calculates = make(map[string]structure.Calculate)
var SymbolSlip []string

// 所有技算符号,按照优先级分组
var SymbolGroup = make(map[int][]string)
var MaxPriority int

// 注册计算器
func RegisterCalculate(c structure.Calculate) {
	//判断是否已经注册过
	if _, ok := Calculates[c.GetName()]; ok {
		panic(errors.New("计算器已经注册过了: " + c.GetName()))
	}
	//添加到策略列表
	Calculates[c.GetName()] = c
	//添加到符号列表
	SymbolGroup[c.GetPriority()] = append(SymbolGroup[c.GetPriority()], c.GetName())
	//获取最大优先级
	if c.GetPriority() > MaxPriority {
		MaxPriority = c.GetPriority()
	}
	//添加到符号切片,按照字符串长度排序从大到小
	SymbolSlip = append(SymbolSlip, c.GetName())
	common.SortSlice[string](&SymbolSlip, 1, func(a, b string) int {
		return len(a) - len(b)
	})
}
